//
//  WJRefreshController.m
//  JerryIos
//
//  Created by 王军 on 2018/2/9.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJRefreshController.h"
#import "MJTableViewController.h"
#import "MJWebViewViewController.h"
#import "MJCollectionViewController.h"
#import "UIViewController+MJRefresh.h"
#import "MJRefresh.h"
#import "WJCarBrand.h"
#import "WJCar.h"

@interface WJRefreshController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *carBrandArray;

@end

@implementation WJRefreshController

- (NSArray *)carBrandArray {
    if (_carBrandArray == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"mj_refresh.plist" ofType:nil];
        NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *groupArray = [NSMutableArray array];
        for (NSDictionary *dic in dicArray) {
            WJCarBrand *carBrand = [WJCarBrand carBrandWithDict:dic];
            [groupArray addObject:carBrand];
        }
        _carBrandArray = groupArray;
    }
    return _carBrandArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建NavBar
    [self setupNavBar];
    // 创建tableView
    [self setupTableView];
}

// 创建NavBar
- (void)setupNavBar {
    //右边的按钮
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"icon_share_normal"]
                                                                  highImage:[UIImage imageNamed:@"icon_share_selected"] target:self
                                                                     action:@selector(share)];
    //中间的标题
    self.navigationItem.title = @"MJRefresh";
    
}

-(void)share{
    
}

// 创建tableView
- (void)setupTableView {
    
    //TableView的Frame
    CGFloat topHeight = [[UIApplication sharedApplication] statusBarFrame].size.height + 44;
    self.tableView.frame = CGRectMake(0, topHeight, self.view.bounds.size.width, self.view.bounds.size.height - topHeight);
    //指定代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.carBrandArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    WJCarBrand *carBrand = self.carBrandArray[section];
    return carBrand.title;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    WJCarBrand *carBrand = self.carBrandArray[section];
    return carBrand.carArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellId];
    }
    WJCarBrand *carBrand = self.carBrandArray[indexPath.section];
    WJCar *car = carBrand.carArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:car.icon];
    cell.textLabel.text = car.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WJCarBrand *carBrand = self.carBrandArray[indexPath.section];
    WJCar *car = carBrand.carArray[indexPath.row];
    
    MJTableViewController *tableViewController = [[MJTableViewController alloc] init];
    MJCollectionViewController *collectionViewController = [[MJCollectionViewController alloc] init];
    MJWebViewViewController *webviewController = [[MJWebViewViewController alloc] init];
    
    switch (indexPath.section) {
        case 0:
        case 1:
            tableViewController.title = car.name;
            [tableViewController setValue:car.method forKeyPath:@"method"];
            [self.navigationController pushViewController:tableViewController animated:YES];
            break;
        case 2:
            collectionViewController.title = car.name;
            [collectionViewController setValue:car.method forKeyPath:@"method"];
            [self.navigationController pushViewController:collectionViewController animated:YES];
            break;
        case 3:
            webviewController.title = car.name;
            [webviewController setValue:car.method forKeyPath:@"method"];
            [self.navigationController pushViewController:webviewController animated:YES];
            break;
        default:
            break;
    }
}

@end
